package com.mingye.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * 图片工具类
 *
 * @author daixinyi
 */
public class ImageUtils {

    /**
     * 根据图片路径，加载图片
     *
     * @param imagePath
     * @return
     */
    public static BufferedImage loadImage(String imagePath) {
        try {
            String rootFilePath = getRootFilePath();
            BufferedImage image = ImageIO.read(new File(rootFilePath + imagePath));
            return image;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 获取图片文件的根路径
     *
     * @return
     */
    private static String getRootFilePath() {
        // 获取当前工作目录
        String projectRoot = System.getProperty("user.dir");
        String filePath = projectRoot + "/images";
        return filePath + File.separator;
    }

}
